﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using S3System.BusinessLogic.CRM;
using S3System.BusinessEntities;
using System.Xml.Linq;
using System.Threading.Tasks;
namespace S3System.S3Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public Customer CurrentCustomer;  
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            CurrentCustomer = new Customer();
            CurrentCustomer.FirstName = this.txtName.Text;
            CurrentCustomer.Id = Guid.NewGuid();
            CurrentCustomer.BirthDate = DateTime.Now;
            CurrentCustomer.LastName = this.txtName.Text;
            CurrentCustomer.MarkAsAdded();
            this.CustomerManagement.MaintenanceCustomer(CurrentCustomer);

            LoadCustomer();
        }

        private void LoadCustomer()
        {
            this.dataGrid1.AutoGenerateColumns = true;

            var input = this.CustomerQueries.QueryCustomers();
            if (input != null)
            {
                var result = XElement.Parse(input);
                var enti = from c in result.Descendants("Customer") 
                           select new { 
                               id = Guid.Parse(c.Element("Id").Value), 
                               nombre = c.Element("FirstName").Value,
                               apellido = c.Element("LastName").Value,
                               birthDate = DateTime.Parse( c.Element("BirthDate").Value)
                           };
                this.dataGrid1.ItemsSource = enti.ToList();
            }
            else
            {
                this.dataGrid1.ItemsSource = null;
 
            }
            
        }
        public IUnityContainer Container = new UnityContainer();

        public IBCCustomerManagement CustomerManagement { get; set; }
        public IBCCustomerQueries CustomerQueries { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var UnitySection = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
                Container.LoadConfiguration(UnitySection);
                this.CustomerManagement = Container.Resolve<IBCCustomerManagement>();
                this.CustomerQueries = Container.Resolve<IBCCustomerQueries>();
                LoadCustomer();
            }
            catch (Exception ex)
            {
                
                
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            

            try
            {
                this.CurrentCustomer.BirthDate = DateTime.Now; 
                this.CurrentCustomer.MarkAsModified();
                this.CustomerManagement.MaintenanceCustomer(this.CurrentCustomer);

                LoadCustomer();
            
            }
            catch (Exception)
            {
                    
                throw;
            }

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {   
            this.CurrentCustomer.MarkAsDeleted();
            this.CustomerManagement.MaintenanceCustomer(this.CurrentCustomer);
            LoadCustomer();
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (this.dataGrid1.SelectedItem != null)
            {
                dynamic item = this.dataGrid1.SelectedItem;

                var customer = new Customer();
                customer.Id = item.id;
                customer.FirstName = item.nombre;
                customer.LastName = item.apellido;
                customer.BirthDate = item.birthDate;

                this.CurrentCustomer = customer; 
            }
            

        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var result = this.CustomerQueries.ValidateConfiguration();
                MessageBox.Show(result);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }
            
        }
    }
}
